/**
 * 发送到测试服务器
 */
var cp = require("child_process");

module.exports = function (next) {
    require("colors");
    require("shelljs/global");

    var updateComposer = require("./updateComposer"),
        deployInfo = require("../../deploy-conf.json"),
        urls = [],
        maps = [];

    if (exec("cd .build/build.json && git pull").code != 0) {
        console.log("\ngit pull error!".red);
        process.exit(0);
    } else {
        console.log("\ngit pull index.json complete".green);
    }

    cp.exec("whoami", function (err) {
        if (err) {
            console.log(err);
            console.log("\nget user error!".red);
        } else {
            urls = deployInfo.deploy;
            maps = urls.map(function (item, index) {
                return "test" + index;
            });

            updateComposer([].concat(urls), function () {
                console.log("\nfisp release to test server...".green);
                exec("fisp release -wp -d " + maps.join(","));

                if (next && next.length) {
                    next.shift()(next);
                }
            }, null, false);
        }
    });
};
